package cn.seaboot.admin.core.web;

import cn.seaboot.admin.core.bean.Dictionary;
import cn.seaboot.admin.core.service.DictionaryService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.SystemConstant;
import cn.seaboot.admin.mvc.argument.ModifyArgument;
import cn.seaboot.admin.mvc.argument.Params;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统字典 [t_sys_dictionary] - Controller
 *
 * @author Mr.css on 2018-6-20
 * @version 1.0.0
 */
@Controller
@RequestMapping("sys/dic")
@Tag(name = "系统字典 [t_sys_dictionary] 管理")
public class DictionaryCtrl {

    @Resource
    private DictionaryService dictionaryService;

    /**
     * 数据字典 - 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典 [t_sys_dictionary] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated Dictionary pojo) {
        return dictionaryService.insert(pojo);
    }

    /**
     * 数据字典 - 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典 [t_sys_dictionary] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params Integer id) {
        return dictionaryService.deleteById(id);
    }

    /**
     * 数据字典 - 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "数据字典 [t_sys_dictionary] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Dictionary queryById(@Params Integer id) {
        return dictionaryService.queryById(id);
    }

    /**
     * 数据字典 - 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典 [t_sys_dictionary] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Validated Dictionary pojo, @Params String id) {
        return dictionaryService.updateById(pojo);
    }

    /**
     * 数据字典 - 改
     *
     * @param arg 字段编辑
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典 [t_sys_dictionary] 改")
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public int updateInfo(@Validated ModifyArgument arg) {
        return dictionaryService.updateFieldById(arg);
    }

    /**
     * 查
     *
     * @param tableName  表名
     * @param columnName 列名
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "系统字典 [t_sys_dictionary] 查询数据字典")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<Dictionary> queryByType(
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName,
            @Params String tableName, @Params String columnName) {
        return dictionaryService.queryByType(schemaName, tableName, columnName);
    }

    /**
     * 查列表
     *
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "系统字典 [t_sys_dictionary] 列表查询")
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<Dictionary> queryPage(
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName,
            HttpServletRequest request) {
        return Pagination.get(request, () -> dictionaryService.selectAll(schemaName));
    }
}
